{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "701e2368-9997-4840-97de-756011b68ed8",
   "metadata": {},
   "source": [
    "## Uniform CI for any Statistic\n",
    "\n",
    "**S1**: Estimate the model of interest, and get the coefficients of interest. Save the variance covariance matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "06e95f13-3cb6-42ac-824f-69413ac9451c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Written by R.              )\n",
      "WARNING: Singleton observations not dropped; statistical significance is biased\n",
      ">  (link)\n",
      "(MWFE estimator converged in 2 iterations)\n",
      "note: 2007.year#c.lpop omitted because of collinearity\n",
      "\n",
      "HDFE Linear regression                            Number of obs   =      2,500\n",
      "Absorbing 2 HDFE groups                           F(  28,    499) =       3.15\n",
      "Statistics robust to heteroskedasticity           Prob > F        =     0.0000\n",
      "                                                  R-squared       =     0.9934\n",
      "                                                  Adj R-squared   =     0.9916\n",
      "                                                  Within R-sq.    =     0.0240\n",
      "Number of clusters (countyreal) =        500      Root MSE        =     0.1386\n",
      "\n",
      "                           (Std. err. adjusted for 500 clusters in countyreal)\n",
      "------------------------------------------------------------------------------\n",
      "             |               Robust\n",
      "        lemp | Coefficient  std. err.      t    P>|t|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      " first_treat#|\n",
      "        year#|\n",
      "    c.__tr__ |\n",
      "  2004 2004  |  -.0149112   .0222198    -0.67   0.502    -.0585672    .0287448\n",
      "  2004 2005  |  -.0769963   .0277681    -2.77   0.006    -.1315531   -.0224395\n",
      "  2004 2006  |  -.1410801   .0322433    -4.38   0.000    -.2044295   -.0777307\n",
      "  2004 2007  |  -.1075443   .0328764    -3.27   0.001    -.1721375    -.042951\n",
      "  2006 2003  |   .0090343   .0302224     0.30   0.765    -.0503444    .0684131\n",
      "  2006 2004  |   .0069683   .0181921     0.38   0.702    -.0287742    .0427108\n",
      "  2006 2006  |   .0007655   .0186329     0.04   0.967    -.0358432    .0373742\n",
      "  2006 2007  |  -.0415356   .0191982    -2.16   0.031    -.0792549   -.0038164\n",
      "  2007 2003  |   .0068961   .0246543     0.28   0.780    -.0415429    .0553351\n",
      "  2007 2004  |   .0332619   .0213008     1.56   0.119    -.0085883    .0751122\n",
      "  2007 2005  |   .0285021   .0182653     1.56   0.119    -.0073843    .0643885\n",
      "  2007 2007  |  -.0287895   .0161312    -1.78   0.075     -.060483     .002904\n",
      "             |\n",
      " first_treat#|\n",
      "        year#|\n",
      "    c.__tr__#|\n",
      "   c._x_lpop |\n",
      "  2004 2004  |   .0005953   .0183556     0.03   0.974    -.0354686    .0366592\n",
      "  2004 2005  |   .0234096   .0183749     1.27   0.203     -.012692    .0595113\n",
      "  2004 2006  |   .0482261   .0224194     2.15   0.032      .004178    .0922742\n",
      "  2004 2007  |   .0091886   .0271423     0.34   0.735    -.0441386    .0625158\n",
      "  2006 2003  |  -.0126074   .0243335    -0.52   0.605    -.0604162    .0352014\n",
      "  2006 2004  |  -.0177865   .0161892    -1.10   0.272    -.0495939    .0140208\n",
      "  2006 2006  |   .0282074   .0141213     2.00   0.046     .0004628     .055952\n",
      "  2006 2007  |   .0277793   .0180844     1.54   0.125    -.0077516    .0633102\n",
      "  2007 2003  |   .0083787   .0254037     0.33   0.742    -.0415327    .0582902\n",
      "  2007 2004  |  -.0079105   .0188673    -0.42   0.675    -.0449797    .0291587\n",
      "  2007 2005  |  -.0025825   .0178299    -0.14   0.885    -.0376135    .0324484\n",
      "  2007 2007  |  -.0203637   .0162117    -1.26   0.210    -.0522153    .0114878\n",
      "             |\n",
      " year#c.lpop |\n",
      "       2003  |  -.0229821   .0129329    -1.78   0.076    -.0483918    .0024277\n",
      "       2004  |  -.0079359   .0107757    -0.74   0.462    -.0291072    .0132355\n",
      "       2005  |  -.0005453   .0097177    -0.06   0.955     -.019638    .0185474\n",
      "       2006  |  -.0099382   .0090048    -1.10   0.270    -.0276302    .0077537\n",
      "       2007  |          0  (omitted)\n",
      "             |\n",
      "       _cons |   5.800979   .0225656   257.07   0.000     5.756644    5.845315\n",
      "------------------------------------------------------------------------------\n",
      "\n",
      "Absorbed degrees of freedom:\n",
      "-----------------------------------------------------+\n",
      " Absorbed FE | Categories  - Redundant  = Num. Coefs |\n",
      "-------------+---------------------------------------|\n",
      "  countyreal |       500         500           0    *|\n",
      "        year |         5           0           5     |\n",
      "-----------------------------------------------------+\n",
      "* = FE nested within cluster; treated as redundant for DoF computation\n",
      "------------------------------------------------------------------------------\n",
      "             |            Delta-method\n",
      "             | Coefficient  std. err.      z    P>|z|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "         _at@|\n",
      "   __event__ |\n",
      "(2 vs 1) -4  |   .0068961   .0246543     0.28   0.780    -.0414254    .0552177\n",
      "(2 vs 1) -3  |   .0275947   .0181227     1.52   0.128    -.0079251    .0631145\n",
      "(2 vs 1) -2  |    .023465   .0145109     1.62   0.106    -.0049758    .0519057\n",
      "(2 vs 1) -1  |          0  (omitted)\n",
      " (2 vs 1) 0  |  -.0211467   .0113774    -1.86   0.063     -.043446    .0011525\n",
      " (2 vs 1) 1  |  -.0533559    .015752    -3.39   0.001    -.0842293   -.0224824\n",
      " (2 vs 1) 2  |  -.1410801   .0322433    -4.38   0.000    -.2042759   -.0778843\n",
      " (2 vs 1) 3  |  -.1075443   .0328764    -3.27   0.001    -.1719809   -.0431077\n",
      "------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "frause mpdta, clear\n",
    "jwdid lemp lpop, ivar( countyreal) tvar( year) gvar( first_treat) never\n",
    "estat event, post\n",
    "matrix b=e(b)\n",
    "matrix V=e(V)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f007542f-40d7-4165-92b4-1eca24cfd97f",
   "metadata": {},
   "source": [
    "**S2**: Draw X repetitions for normal distribution from the model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d11e810a-40b7-4d57-8507-4263713c78c4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(obs 9,999)\n"
     ]
    }
   ],
   "source": [
    "capture frame create scu\n",
    "frame scu: {\n",
    "drawnorm x1 x2 x3 x4 x5 x6 x7 x8, cov(V) n(9999)\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da369616-5032-4d3f-bbd0-821094224f80",
   "metadata": {
    "tags": []
   },
   "source": [
    "**S3**: Estimate t-Stats, and get the max of the absolute distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a970ae98-3451-491a-a5d9-55aa1cb0395b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(9,999 real changes made)\n",
      "(9,999 real changes made)\n",
      "(9,999 real changes made)\n",
      "(9,999 real changes made, 9,999 to missing)\n",
      "(9,999 real changes made)\n",
      "(9,999 real changes made)\n",
      "(9,999 real changes made)\n",
      "(9,999 real changes made)\n"
     ]
    }
   ],
   "source": [
    "frame scu:{\n",
    "    mata: vv=st_matrix(\"V\")\n",
    "    mata: se=diagonal(vv)':^.5\n",
    "    mata: st_matrix(\"se\",se)\n",
    "    forvalues i =1/8 {\n",
    "        replace x`i'=abs( x`i'/se[1,`i'])\n",
    "    }\n",
    "    egen tmax = rowmax(x*)\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7564876e-983e-4015-802e-43700bdc19a4",
   "metadata": {},
   "source": [
    "**S4**: The 95th percentile of tmax is the new tvalue to construct uniform CI\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6286b391-2297-4ecf-a6c8-0db31d3c78dc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "                            tmax\n",
      "-------------------------------------------------------------\n",
      "      Percentiles      Smallest\n",
      " 1%     .5793542       .2684478\n",
      " 5%      .795673       .3164798\n",
      "10%     .9245483       .3388914       Obs               9,999\n",
      "25%     1.193421       .3665175       Sum of wgt.       9,999\n",
      "\n",
      "50%     1.557338                      Mean           1.609948\n",
      "                        Largest       Std. dev.      .5672732\n",
      "75%     1.957006       4.280075\n",
      "90%     2.371118        4.30576       Variance       .3217989\n",
      "95%     2.638112       4.532749       Skewness       .6199499\n",
      "99%     3.150532       4.742303       Kurtosis       3.496845\n"
     ]
    }
   ],
   "source": [
    "frame scu:sum tmax,d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "913a9cb1-2e35-4f63-a0b7-92887514f4ad",
   "metadata": {},
   "source": [
    "So, you just need to construct your new CI using the same SE but with this new t-critical at 95% 2.638112"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Stata (nbstata)",
   "language": "stata",
   "name": "nbstata"
  },
  "language_info": {
   "file_extension": ".do",
   "mimetype": "text/x-stata",
   "name": "stata",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
